<template>
  <div>
    <!-- 顶部 -->
    <el-card>
      <Category @getCategoryId="getCategoryId"> </Category>
    </el-card>
    <!-- 底部 -->
    <el-card style="margin: 20px 0">
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!category3Id"
          @click="addAttr"
          >添加属性</el-button
        >
        <!-- table -->
        <el-table style="width: 100%; margin: 20px 0" border :data="attrList">
          <el-table-column
            type="index"
            label="序号"
            width="80px"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="200px">
          </el-table-column>
          <el-table-column label="属性值列表" width="width">
            <template slot-scope="{ row, $index }">
              <!-- 展示平台属性 -->
              <el-tag
                hit
                type="success"
                v-for="attrValue in row.attrValueList"
                :key="attrValue.id"
                style="margin: 0 5px"
                >{{ attrValue.valueName }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="200px">
            <template slot-scope="{ row, $index }">
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="updateAttr(row)"
              ></el-button>
              <el-popconfirm title="确定的删除吗?" @onConfirm="deleteAttr(row)">
                <el-button
                style="margin: 0 5px"
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  slot="reference"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div v-show="scene == 1">
        <el-form :inline="true" ref="form">
          <el-form-item label="属性名">
            <el-input v-model="attrForm.attrName"></el-input>
          </el-form-item>
        </el-form>
        <!-- 按钮 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addAttrValue"
          :disabled="!attrForm.attrName"
          >添加属性值</el-button
        >
        <el-button @click="scene = 0">取消</el-button>
        <!-- table -->
        <el-table
          border
          style="width: 100%; margin: 20px 0"
          :data="attrForm.attrValueList"
        >
          <el-table-column type="index" align="center" label="序号" width="80">
          </el-table-column>
          <el-table-column label="属性值名称" width="width">
            <template slot-scope="{ row, $index }">
              <!-- 实现编辑模式与查看模式的切换 -->
              <el-input
                :ref="$index"
                v-model="row.valueName"
                size="mini"
                v-if="row.show"
                @keyup.enter.native="toLook(row)"
                @blur="toLook(row)"
              ></el-input>
              <div v-else @click="toEdit(row, $index)">{{ row.valueName }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="attrForm.attrValueList.splice($index, 1)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 按钮 -->
        <el-button
          type="primary"
          :disabled="!(attrForm.attrValueList.length > 0)"
          @click="addOrUpdateSave"
          >保存</el-button
        >
        <el-button @click="scene = 0">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
// 引入神考别
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "Attr",
  data() {
    return {
      // 存储子组件传递过来的数据
      category1Id: "",
      category2Id: "",
      category3Id: "",
      // 存储平台属性
      attrList: [],
      // 控制显示内容的切换
      scene: 0,
      // 收集数据=将来发请求携带参数给服务器的
      attrForm: {
        // 属性的名字
        attrName: "",
        // 属性指的地方
        attrValueList: [
          // {
          //   attrId: 0,//属性指的id
          //   valueName: "",//属性指的名称
          // },
        ],
        categoryId: "", //代表的是三级分类的id
        categoryLevel: 3, //带给服务器的
      },
    };
  },
  methods: {
    // 自定义事件获取1 2 3 id
    getCategoryId({ id, flag }) {
      // 判断是几id
      switch (flag) {
        case 1:
          this.category1Id = id;
          this.category2Id = "";
          this.category3Id = "";
          break;
        case 2:
          this.category2Id = id;
          this.category3Id = "";
          break;
        case 3:
          this.category3Id = id;
          // 发请求获取品牌属性的数据
          this.getAttrList();
          break;
      }
    },
    // 获取品台属性的函数
    async getAttrList() {
      let { category1Id, category2Id, category3Id } = this;
      let result = await this.$ajax.attr.reqAttrList(
        category1Id,
        category2Id,
        category3Id
      );
      if (result.code == 200) {
        this.attrList = result.data;
      }
    },
    // 添加按钮属性的回调
    addAttr() {
      this.scene = 1;
      // 清空数据
      this.attrForm = {
        // 属性的名字
        attrName: "",
        // 属性指的地方
        attrValueList: [],
        categoryId: this.category3Id, //代表的是三级分类的id
        categoryLevel: 3, //带给服务器的
      };
    },
    // 修改属性的回调
    updateAttr(row) {
      this.scene = 1;
      this.attrForm = cloneDeep(row);
      //  添加响应式数据
      // 第一种使用$set***********************************************
      // this.attrForm.attrValueList.forEach(item=>{
      //   this.$set(item,'show',false)
      // })
      // 第二种方法
      this.attrForm.attrValueList = this.attrForm.attrValueList.map((item) => {
        return {
          ...item,
          show: false,
        };
      });
    },
    // 添加属性值的回调
    addAttrValue() {
      // 向数组添加元素
      this.attrForm.attrValueList.push({
        attrId: this.attrForm.id, //属性指的id
        valueName: "", //属性指的名称
        // 展示input 与span
        show: true,
      });
      // 获取input组件事件让他聚焦[数组尾巴元素]
      this.$nextTick(() => {
        this.$refs[this.attrForm.attrValueList.length - 1].focus();
      });
    },
    // 切换产看模式
    toLook(row) {
      // 当失去焦点的时候属性值不能为空
      if (row.valueName.trim() == "") {
        this.$message({
          type: "warning",
          message: "输入的内容不能为空",
        });
        return;
      }
      // 判断是否重复
      let repeat = this.attrForm.attrValueList.find((item) => {
        // 判断是否重复:将用户当前操作的这个属性值抛出去
        if (item != row) {
          return item.valueName == row.valueName;
        }
      });
      if (repeat) {
        this.$message({
          type: "warning",
          message: "请不要重复输入内容",
        });
        return;
      }
      row.show = false;
    },
    // 切换为编辑模式
    toEdit(row, index) {
      row.show = true;

      // 通过ref获取input组件让它聚焦
      this.$nextTick(() => {
        // 获取input组件
        this.$refs[index].focus();
      });
    },
    // 添加属性|||修改属性的保存按钮-发请求
    async addOrUpdateSave() {
      // 整理参数 属性值为空的不带给服务器 show字段移除
      this.attrForm.attrValueList = this.attrForm.attrValueList.filter(
        (item) => {
          // 带给服务器的参数:valueName属性值不能为空
          if (item.valueName != "") {
            delete item.show;
            return true;
          }
        }
      );
      // 发请求:新增\修改
      try {
        // 新增成功
        await this.$ajax.attr.reqAddOrUpdateAttr(this.attrForm);
        // 切换场景
        (this.scene = 0),
          // 获取平台属性的最新数据
          this.getAttrList();
        this.$message({
          type: "success",
          message: "添加成功",
        });
      } catch (error) {}
    },
    // 删除的业务
   async deleteAttr({id}){
     try {
       await this.$ajax.attr.reqDeleteAttrById(id)
       this.getAttrList();
       this.$message({
         type: "success",
         message: "删除成功"
       })
     } catch (error) {
       this.$message({
         type: "error",
         message: "删除失败"
       })
     }
    }
  },
};
</script>

<style>
</style>